package eclipse.util.maptools;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import eclipse.lib.graphics.*;
import eclipse.util.*;
/** Class that handles OpenGL calls within the Map Editor */
public class MapWindow implements GLEventListener, WindowListener
{	
	/** Interface between Java and OpenGL */
	public GLInterface 		glInterface;
	/** A new window that is drawn to */
	private JFrame			window;
	/** Creates a MapWindow */
	public MapWindow()
	{
		window = new JFrame("Map View");
		window.addWindowListener(this);
		window.setSize(640, 480);
		window.setLocation(0, 0);
		window.setVisible(true);
		glInterface = new GLInterface(window, this);
	}
	/** 
	 * Sets the title of the Window.
	 */
	public void setTitle(String title)
	{
		window.setTitle(title);
	}
	public void display(GLAutoDrawable GLDraw) 
	{
		GL gl = GLDraw.getGL();			
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);  
		if(MapEditor.map.length == 0)
			return;
		if(MapEditor.map[MapEditor.currentMap] == null)
			return;
		if(MapEditor.map[MapEditor.currentMap].getLayerCount() == 0)
			return;
		
		gl.glPushMatrix();
			if(MapEditor.toolkit.wireframe.isSelected())
				gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
			else
				gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);
			for(int x = 0; x < MapEditor.map[MapEditor.currentMap].
				getLayer(MapEditor.currentLayer).getMaxX(); x ++)
			{
				for(int y = 0; y < MapEditor.map[MapEditor.currentMap].
					getLayer(MapEditor.currentLayer).getMaxY(); y++)
				{				
					gl.glPushMatrix();
						gl.glTranslatef(x * 32f, y * 32f, 0f);
						if(MapEditor.map[MapEditor.currentMap].getLayer(MapEditor.currentLayer).getTileX(x, y) == 0)
							if(MapEditor.map[MapEditor.currentMap].getLayer(MapEditor.currentLayer).getTileY(x, y) == 0)
								if(MapEditor.map[MapEditor.currentMap].getLayer(MapEditor.currentLayer).getTileSheet(x,y) == 0)														
									GLPrimitives.drawTile(gl, MapEditor.tBlank, 0, 0);
														
					gl.glPopMatrix();
				}
			}
		gl.glPopMatrix();
	}
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) 
	{	
		
	}
	public void init(GLAutoDrawable GLDraw) 
	{
		GL gl = GLDraw.getGL(); 			
	    
		gl.glShadeModel(GL.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepth(1.0f);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glEnable(GL.GL_TEXTURE_2D);
				
		gl.glDepthFunc(GL.GL_LEQUAL);		
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST);				
		MapEditor.tBlank = GLTextureLoader.loadIMG(new ERL("jar://gfx/blank"), false);
	}
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) 
	{		
		@SuppressWarnings("unused")
		double left, right, bottom, top, zNear, zFar, fov, aspect;
		fov 	= 45d;
		aspect 	= (double) width / (double) height;
		zNear 	= 0.1d;
		zFar  	= 1000.0d;
					
		top 	= Math.tan(fov* Math.PI /360) * zNear;
		bottom 	= -top;
		left 	= aspect * bottom;
		right 	= aspect * top;
		
		GL gl = drawable.getGL();			
		gl.setSwapInterval(1);
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();			
		//gl.glFrustum(left, right, bottom, top, zNear, zFar);
		gl.glOrtho(0d, 640d, 480d, 0d, 0d, 1000d);
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
	}
	public void windowActivated(WindowEvent arg0) 
	{
		
	}
	public void windowClosed(WindowEvent arg0) 
	{
		System.exit(0);
	}
	public void windowClosing(WindowEvent arg0) 
	{
		System.exit(0);
	}
	public void windowDeactivated(WindowEvent arg0) 
	{
		
	}
	public void windowDeiconified(WindowEvent arg0) 
	{
		
	}
	public void windowIconified(WindowEvent arg0) 
	{
		
	}
	public void windowOpened(WindowEvent arg0) 
	{
		
	}
}
